//
//  EditTextSubView.m
//  QRCode
//
//  Created by zhibao on 2023/2/17.
//

#import "SelectTextSubView.h"
#import "TZTestCell.h"
@interface SelectTextSubView ()<UICollectionViewDataSource,UICollectionViewDelegate>{
    CGFloat _itemWH;
    CGFloat _margin;
    BOOL flag;
}
@property (nonatomic, strong) MYCollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation SelectTextSubView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        flag = YES;
        _dataArray = [NSMutableArray new];
        [self initView];
    }
    return self;
}

- (void)reloadDataWithDict:(NSDictionary *)dict{
    if(flag){
        flag = NO;
        NSString *type = LocalizedString(@"字体颜色");
        NSArray *colors = @[@"#333333",@"#027AFF",@"#449C53",@"#207876",@"#9F9F15",@"#07A395",@"#B20796",@"#34208F",@"#631A80"];
        NSDictionary *clearDic = @{
            @"img":@"trash",
            @"value":CLEARFLAG
        };
        [_dataArray addObject:clearDic];
        for (int i = 0; i < colors.count; i ++) {
            NSDictionary *dic = @{
                @"type":type,
                @"value":colors[i]
            };
            [_dataArray addObject:dic];
        }
        [_collectionView reloadData];
    }
}

- (void)initView {
    // 如不需要长按排序效果，将LxGridViewFlowLayout类改成UICollectionViewFlowLayout即可
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _collectionView = [[MYCollectionView alloc] initWithFrame:self.bounds collectionViewLayout:_layout];
    _collectionView.alwaysBounceVertical = NO;
    DLog(@"eeee====%f",self.height);
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.contentInset = UIEdgeInsetsMake(0, 10, 10, 0);
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [_collectionView removeMJRefresh];
    _collectionView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    [self addSubview:_collectionView];
    [_collectionView setNoDataType:TableViewNoDataType_FILE];
    [_collectionView.mj_footer endRefreshingWithNoMoreData];
    [_collectionView registerClass:[TZTestCell class] forCellWithReuseIdentifier:@"TZTestCell"];
    _layout.itemSize = CGSizeMake(pixw(30), pixw(30));
    _margin = 10;
    _layout.minimumInteritemSpacing = _margin;
    _layout.minimumLineSpacing = _margin;
    [self.collectionView setCollectionViewLayout:_layout];
    [_collectionView reloadData];
}

#pragma mark UICollectionView

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TZTestCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZTestCell" forIndexPath:indexPath];
    cell.imageView.image = nil;
    NSDictionary *dic = _dataArray[indexPath.row];
    cell.imageView.image = nil;
    cell.imageView.backgroundColor = MyColor(dic[@"value"]);
    if(indexPath.row == 0){
        if(_dataArray[indexPath.row][@"img"]){
            cell.imageView.backgroundColor = UIColor.whiteColor;
            cell.imageView.image = [UIImage systemImageNamed:_dataArray[indexPath.row][@"img"]];
        }
    }
    cell.deleteBtn.hidden = YES;
    cell.gifLable.hidden = YES;
    cell.deleteBtn.tag = indexPath.item;
    return cell;
    
}

#pragma mark - Click Event
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if(self.selectColorSubBlock){
        self.selectColorSubBlock(_dataArray[indexPath.row]);
    }
}
@end

